﻿// ==UserScript==
// @name          BaiduMp3
// @description	  download baidu Mp3 directly
// @include       http://mp3.baidu.com/m*
// ==/UserScript==

var sertim;

var allLinks = document.evaluate("//div[@id='leftRes']/table[@id='Tbs']/tbody/tr/td[@class='d']/a",document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null); 

if(allLinks.snapshotLength > 0) {
  for(var i = 0; i < allLinks.snapshotLength; i++) {
      var myA = allLinks.snapshotItem(i).cloneNode(false);
      myA.innerHTML = "\uFEFF\u4E0B\u8F7D";
      myA.setAttribute('title',"\uFEFF\u70B9\u51FB\u5DE6\u952E\u76F4\u63A5\u4E0B\u8F7D");
      myA.setAttribute('style','margin-left: 20px !important;');
      harnack(myA);
      allLinks.snapshotItem(i).parentNode.appendChild(myA);
  }
}

function harnack(aLink) {
      GM_xmlhttpRequest({
    	method: 'GET',
    	url: aLink.href,
  	onload: function(xhr) {
          var text = xhr.responseText;
  	  B = text.match(/var B=".*"/)[0].replace(/var B=|"/g,"");
  	  sertim = text.match(/var sertim=\d+,/)[0].replace(/\D/g,"");
  	  TrueLink = mydecode(B).match(/.*?,/)[0].replace(/,/,"");
          aLink.href = TrueLink; /* 有时网址会有乱码，可能是因为编码关系 */
        }
      });

}

function mydecode(B) { var E = B.length; var C = ""; var I = [], F = []; var J = sertim % 26; J = J ? J : 1; function K(O, L, M) { for (var N = O; N <= L; N++) { I[N] = N + M; F[N + M] = N; } } K(0, 9, 48); K(10, 35, 55); K(36, 61, 61); for (var D = 0; D < E; D++) { var A = B.charAt(D); if (/[A-Za-z0-9]/.test(A)) { var H = F[B.charCodeAt(D)] - J; if (H < 0) { H += 62; } A = String.fromCharCode(I[H]); } C += A; } return C; }